package cn.rocky.prepare;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class longestNiceSubarray {

    public static int longestNiceSubarray(int[] nums) {
        int min = 1;
        int max = 1;
        List<Integer> list = new ArrayList<>();
        list.add(nums[0]);
        for (int i = 1; i < nums.length; i++) {
            if ((nums[i] & nums[i-1]) == 0) {
                boolean flag = true;
                for (int j = 0; j < list.size(); j++) {
                    if ((nums[i] & list.get(j)) != 0) {
                        min = 1;
                        list.clear();
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    min++;
                    max = Math.max(min,max);
                }
            } else {
                min = 1;
                list.clear();
            }
            list.add(nums[i]);
        }
        return max;
    }

    public static int longestNiceSubarray1(int[] nums) {
        int ans = 0;
        for (int i = 0; i < nums.length; i++) {
            int or = 0, j = i;
            while (j >= 0 && (or & nums[j]) == 0) {
                or |= nums[j--];
            }
            ans = Math.max(ans, i - j);
        }
        return ans;
    }

    public static void main(String[] args) {
        int[] num = new int[]{3,1,5,11,13};
        int[] num1 = new int[]{1,3,8,48,10};
        int[] num2 = new int[]{84139415,693324769,614626365,497710833,615598711,264,65552,50331652,1,1048576,16384,544,
                270532608,151813349,221976871,678178917,845710321,751376227,331656525,739558112,267703680};
//        System.out.println(longestNiceSubarray(num));
        System.out.println(longestNiceSubarray1(num2));
//        System.out.println(3 & 10);
//        System.out.println(3 & 8);
//        System.out.println(48 & 8);
//        System.out.println(8 & 10);
//        System.out.println(48 & 10);
        int or = 0;
        or = or | 48;
        System.out.println(or);
        or = or | 8;
        System.out.println(or);
        or = or | 3;
        System.out.println(or);
    }
}
